package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.common.format
import io.xxx.omni.oss.common.toJsonString
import io.xxx.omni.oss.common.toLocalDateTime
import io.xxx.omni.oss.domain.Document
import io.xxx.omni.oss.domain.DocumentType
import io.xxx.omni.oss.sdk.XhsClient
import java.util.*

@Suppress("unused")
class XhsTradeSynchronizer : XhsSynchronizer() {

    override val documentType = DocumentType.REFUND

    override fun getResponse(context: Context, pageNo: Int, containsTotal: Boolean): JsonObject {
        val clientName = "${platform.id}Client${store.id}"
        val client = applicationContext.getBean(clientName, XhsClient::class.java)
        return client.execute(store, "", buildRequest(context, pageNo))
    }

    override fun buildDocuments(context: Context, response: JsonObject): List<Document> {
        return response.getAsJsonArray("packageList")
            .map {
                it as JsonObject
                Document(
                    it["orderId"].asString,
                    it["packageStatus"].asString,
                    it.toJsonString(),
                    it["createTime"].toLocalDateTime(),
                    it["updateTime"].toLocalDateTime(),
                )
            }
    }
}